package com.dalongxin.work.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;

import com.dalongxin.work.R;
import com.dalongxin.work.view.LoadingProgressDialog;

import java.util.Random;

public class ProgressDialogUtils {

	private static LoadingProgressDialog mLoadingProgressDialog; // 等待加载框

	public static void showProgressDialog(int resId, Context context) {
		showProgressDialog(context.getString(resId), context);
	}

	public static void showProgressDialog(int resId, Context context, Boolean cancelable) {
		showProgressDialog(context.getString(resId), context, cancelable);
	}

	public static void showProgressDialog(String msg, Context context) {
		showProgressDialog(msg, context, true);
	}

	public static void showProgressDialog(String msg, Context context, Boolean cancelable) {

		/*String[] mLoadingTips = context.getResources().getStringArray(R.array.loading_tips);
		Random tipsRandom = new Random();
		showLoadingProgressDialog(context, TextUtils.isEmpty(msg) ?
				mLoadingTips[tipsRandom.nextInt(mLoadingTips.length)] : msg, cancelable);*/
		showLoadingProgressDialog(context, msg, cancelable);

	}
	public static void dismissProgressBar() {
//		if (proDialog != null && proDialog.isShowing()) {
//			proDialog.dismiss();
//		}
		dismissLoadingProgressDialog();
	}

	/**
	 * 显示加载框
	 * @param context
	 * @param message
	 */
	public static void showLoadingProgressDialog(Context context, String message, boolean cancelable) {
		if (loadingDialogIsShowing()) {
			return;
		}

//		if (mLoadingProgressDialog == null) {
			mLoadingProgressDialog = new LoadingProgressDialog(context, R.style.LoginLoadingDialogStyle);
			mLoadingProgressDialog.setCancelable(cancelable);
//		}
		if (!mLoadingProgressDialog.isShowing()) {
			mLoadingProgressDialog.setMessage(message);
			mLoadingProgressDialog.show();
		}
	}

	/**
	 * 取消加载框
	 */
	public static void dismissLoadingProgressDialog() {
		if (mLoadingProgressDialog != null && mLoadingProgressDialog.isShowing()) {
			mLoadingProgressDialog.dismiss();
			cleanLoadingDialog();
		}
	}

	/**
	 * 回收加载dialog对象
	 *
	 */
	public static void cleanLoadingDialog() {
		if (mLoadingProgressDialog != null) {
			mLoadingProgressDialog = null;
		}
	}

	/**
	 * 判断当前是否有加载框展示
	 * @return
	 */
	private static boolean loadingDialogIsShowing() {
		if (mLoadingProgressDialog != null && mLoadingProgressDialog.isShowing()) {
			return true;
		} else {
			return false;
		}
	}

}
